package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysBusiness;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysOption;
import com.ruoyi.project.system.domain.SysRecord;
import com.ruoyi.project.system.service.ISysBusinessService;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysOptionService;
import com.ruoyi.project.system.service.ISysRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author theme
 * @date 2020-05-27
 */
@Api(description = "recordController",tags = "查询报名信息")
@RestController
@RequestMapping("/mobileUser/record")
public class UserRecordController extends BaseController {
    @Autowired
    private ISysRecordService sysRecordService;
    @Autowired
    private ISysOptionService sysOptionService;
    @Autowired
    private ISysMobileUserService sysMobileUserService;
    @Autowired
    private ISysBusinessService sysBusinessService;

    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation("查询最新注册人列表")
    @GetMapping("/getList")
    public AjaxResult getList() {
        List<SysRecord> sysRecords = sysRecordService.selectSysRecordListMobileUser();
        List<SysMobileUser> list = new ArrayList<>();
        for (int i = 0; i < sysRecords.size(); i++) {
            SysRecord sysRecord = sysRecords.get(i);
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(sysRecord.getMobileUserId());
            if (null != sysMobileUser) {
                Integer time = Integer.valueOf(sysMobileUser.getTime());
                if (time < 60) {
                    sysMobileUser.setTime(time + "分钟");
                }
                if (time >= 60) {
                    sysMobileUser.setTime((time / 60) + "小时");
                }
                if (time / 60 >= 24) {
                    sysMobileUser.setTime((time / 60 / 24) + "天");
                }
                Long mobileUserId = sysMobileUser.getMobileUserId();
                List<SysRecord> records = sysRecordService.selectSysRecordByMobileUserId(mobileUserId);
                List<SysRecord> recordList = new ArrayList<>();
                for (int j = 0; j < records.size(); j++) {
                    Long optionId = records.get(j).getOptionId();
                    SysOption sysOption = sysOptionService.selectSysOptionById(optionId);
                    records.get(j).setSysOption(sysOption);
                    recordList.add(records.get(j));
                }
                sysMobileUser.setRecords(recordList);
                list.add(sysMobileUser);
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation("报名")
    @Log(title = "报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRecord sysRecord) {
        UUID uuid = UUID.randomUUID();
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysRecord.setMobileUserId(mobileUserId);
        List<SysOption> optionIds = sysRecord.getOptionIds();
        String timeMillis = String.valueOf(System.currentTimeMillis());
        for (int i = 0; i < optionIds.size(); i++) {
            SysOption sysOption = optionIds.get(i);
            sysRecord.setProvalue(sysOption.getValue());
            sysRecord.setOptionId(sysOption.getOptionId());
            sysRecord.setUuid(timeMillis);
            sysRecordService.insertSysRecord(sysRecord);
        }
        SysMobileUser sysMobileUser = new SysMobileUser();
        sysMobileUser.setMobileUserId(mobileUserId);
        sysMobileUser.setCreateTime(DateUtils.getNowDate());
        int i = sysMobileUserService.updateSysMobileUser(sysMobileUser);

        //增加报名人数
/*        SysBusiness sysBusiness = sysBusinessService.selectSysBusinessById(sysRecord.getBusinessId());
        int person = Integer.parseInt(sysBusiness.getPerson());
        String personAdd = String.valueOf(person + 1);
        sysBusiness.setPerson(personAdd);
        sysBusinessService.updateSysBusiness(sysBusiness);*/

        return AjaxResult.success("报名成功！");
    }

    @ApiOperation("获取已填写信息")
    @GetMapping("/getValue")
    public AjaxResult getValue() {
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        List<SysRecord> sysRecords = sysRecordService.selectSysRecordByMobileUserId(mobileUserId);
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < sysRecords.size(); i++) {
            SysRecord sysRecord = sysRecords.get(i);
            String provalue = sysRecord.getProvalue();
            SysOption sysOption = sysOptionService.selectSysOptionById(sysRecord.getOptionId());
            String name = sysOption.getName();
            Long sort = sysOption.getSort();
            Map<String, Object> map = new HashMap<>();
            map.put("表格名称", name);
            map.put("排序", sort);
            map.put("内容", provalue);
            list.add(map);
        }
        return AjaxResult.success(list);
    }


    @ApiOperation("查询填写过信息的列表")
    @GetMapping("/recordListMobileUser")
    public AjaxResult record(SysMobileUser sysMobileUser) {
/*        List<SysMobileUser> list = sysMobileUserService.selectSysMobileUserList(sysMobileUser);
        List<SysMobileUser> sysMobileUserList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Long mobileUserId = list.get(i).getMobileUserId();
            List<SysRecord> sysRecords = sysRecordService.selectSysRecordByMobileUserId(mobileUserId);
            List<SysRecord> recordList = new ArrayList<>();
            for (int j = 0; j < sysRecords.size(); j++) {
                Long optionId = sysRecords.get(j).getOptionId();
                SysOption sysOption = sysOptionService.selectSysOptionById(optionId);
                sysRecords.get(j).setSysOption(sysOption);
                recordList.add(sysRecords.get(j));
            }
            list.get(i).setRecords(recordList);
            sysMobileUserList.add(list.get(i));
        }*/

        SysRecord record=new SysRecord();
        record.setMobileUserId(sysMobileUser.getMobileUserId());
        List<SysOption> optionList = sysOptionService.selectOptionList();
        List<SysRecord> recordList = sysRecordService.selectRecordByMobileUserId(record);
        for (SysRecord sysRecord : recordList) {
            Long optionId = sysRecord.getOptionId();
            for (SysOption sysOption : optionList) {
                Long optionId1 = sysOption.getOptionId();
                if (optionId.equals(optionId1)) {
                    sysRecord.setSysOption(sysOption);
                }
            }
        }
        return AjaxResult.success(recordList);
    }

}
